//
//  CJSDataBase.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/30.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSDataBase.h"
#import <sqlite3.h>
#import "FMDatabase.h"

#define dataBasePath [[(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)) lastObject]stringByAppendingPathComponent:dataBaseName]
#define dataBaseName @"chenjinshan.sqlite"

@interface CJSDataBase ()

@property (nonatomic, retain) FMDatabase *db;

@end

@implementation CJSDataBase

+ (instancetype)shareDataBase {
    
    static CJSDataBase *dateBase = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateBase = [[CJSDataBase alloc] init];
    });
    
    return dateBase;
}


#pragma mark 打开数据库
- (void)openDB {
    
    NSLog(@"%@", dataBasePath);
    self.db = [FMDatabase databaseWithPath:dataBasePath];
    if ([self.db open]) {
        
        BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS likeList (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, detail TEXT NOT NULL, type TEXT NOT NULL);"];
        
        if (result) {
            NSLog(@"创表成功");
        }else{
            NSLog(@"创表失败");
        }
        [self.db close];
    }
}

#pragma mark 插入数据
- (void)insertData:(NSString *)title detail:(NSString *)detail type:(NSString *)type {
    
    if ([self.db open]) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO likeList (title, detail, type) VALUES ('%@', '%@', '%@')", title, detail, type];
        
        BOOL result = [self.db executeUpdate:sql];
        if (result) {
            
            NSLog(@"插入成功");
            
        }else
        {
            NSLog(@"插入失败");
            
        }
        [self.db close];
    }
    
}

#pragma mark 删除数据
- (void)deleteData:(NSString *)detail type:(NSString *)type {
    
    if ([self.db open]) {
        NSString *deleteSql = [NSString stringWithFormat:
                               @"DELETE FROM likeList WHERE detail = '%@' and type = '%@'", detail, type];
        BOOL res = [self.db executeUpdate:deleteSql];
        
        if (res) {
            NSLog(@"删除成功");
        } else {
            NSLog(@"删除失败");
        }
        [self.db close];
    }
    
}

#pragma mark 查询数据
- (CJSLikeList *)selectDataWithDetail:(NSString *)detail type:(NSString *)type {
    
    CJSLikeList *likeL = nil;
    if ([self.db open]) {
        FMResultSet *resultSet = [self.db executeQuery:[NSString stringWithFormat:@"SELECT * FROM likeList WHERE detail = '%@' and type = '%@'", detail, type]];
        
        //2.遍历结果集合
        while ([resultSet next]) {
            
            //        int idNum = [resultSet intForColumn:@"id"];
            NSString *title = [resultSet objectForColumnName:@"title"];
            NSString *detail = [resultSet objectForColumnName:@"detail"];
            NSString *type = [resultSet objectForColumnName:@"type"];
            
            likeL = [[CJSLikeList alloc] init];
            likeL.title = title;
            likeL.detail = detail;
            likeL.type = type;
        }
        [self.db close];
    }
    return likeL;
}

#pragma mark 查询数据
- (NSMutableArray *)selectData {
    
    NSMutableArray *array = [NSMutableArray array];
    if ([self.db open]) {
        FMResultSet *resultSet = [self.db executeQuery:[NSString stringWithFormat:@"SELECT * FROM likeList"]];
        
        //2.遍历结果集合
        while ([resultSet next]) {
            
            //        int idNum = [resultSet intForColumn:@"id"];
            NSString *title = [resultSet objectForColumnName:@"title"];
            NSString *detail = [resultSet objectForColumnName:@"detail"];
            NSString *type = [resultSet objectForColumnName:@"type"];
            
            CJSLikeList *likeL = [[CJSLikeList alloc] init];
            likeL.title = title;
            likeL.detail = detail;
            likeL.type = type;
            
            [array addObject:likeL];
        }
        [self.db close];
    }
    return array;
}

@end
